<?php 
Class formsController extends appController
{

	public function submit()
	{
		$result=array('error'=>1,'content'=>'');
		$info['name']=isset($_POST['name'])?hy_check($_POST['name'],'ur'):'';
		$info['email']=isset($_POST['email'])?hy_check($_POST['email'],'e'):'';
		$info['phone']=isset($_POST['phone'])?hy_check($_POST['phone'],'n'):'';
		$info['content']=isset($_POST['content'])?hy_substr($_POST['content'],'255'):'';
		$info['ip']=hy_getIP();
		$info['created']=$_SERVER['REQUEST_TIME'];
		
		$this->assign_global();
		
		
		if($_POST['name']==''||$_POST['email']==''||$_POST['phone']==''||$_POST['content']=='')
		{
			$result['content']='请填写完整';
			print_r(json_encode($result));
			exit;
		}
		if($info['name']=='#@_error')
		{
			$result['content']='名字格式错误';
			print_r(json_encode($result));
			exit;
		}
		if($info['email']=='#@_error')
		{
			$result['content']='邮件格式错误';
			print_r(json_encode($result));
			exit;
		}
		if($info['phone']=='#@_error')
		{
			$result['content']='电话格式错误';
			print_r(json_encode($result));
			exit;
		}
		$check=Model('forms')->check_ip(array('ip'=>$info['ip'],'table'=>'forms_log_1'));
		if(!$check)
		{
			$result['content']='你提交得太频繁了';
			print_r(json_encode($result));
			exit;
		}
		$res=Model('forms')->insert($info,'forms_log_1');
		$mail=new PHPMailer();
		$mail->IsSMTP();                                      // Set mailer to use SMTP
		$mail->Host = C('smtp');  // Specify main and backup server
		$mail->SMTPAuth = true;                               // Enable SMTP authentication
		$mail->Username = C('smtp_username');                            // SMTP username
		$mail->Password = C('smtp_password');                           // SMTP password
		
		$mail->From = C('smtp_email');
		//$mail->FromName = 'Vancca';
		$mail->AddAddress('990248639@qq.com', 'Josh Adams');  // Add a recipient
		$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
		
		$mail->Subject = '留言';
		$mail->Body    = '<p>名称：'.$info['name'].'</p><p>邮箱：'.$info['email'].'</p><p>电话:'.$info['phone'].'</p><p>留言:'.$info['content'].'</p>';
		$mail->AltBody = '<p>名称：'.$info['name'].'</p><p>邮箱：'.$info['email'].'</p><p>电话:'.$info['phone'].'</p><p>留言:'.$info['content'].'</p>';
		$mail->Send();
		
		if($res)
		{
			$result['error']=0;
			$result['content']='添加成功';
		}
		else
		{
			$result['content']='系统繁忙';
		}
		print_r(json_encode($result));
		exit;
	}
	
}